package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.entity.Comment;
import com.itheima.result.Result;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论管理
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return PageInfo<Movies>
     */
    @GetMapping("list")
    public Result findByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            String creatorName,
            String title) {
        //调用service完成分页查询
        PageInfo<Comment> list = commentService.findByPage(pageNum, pageSize, creatorName, title);
        return Result.success(list);
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return movies
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        Comment comment = commentService.findById(id);
        return Result.success(comment);
    }

    /**
     * 新增或修改
     *
     * @param comment 电影对象
     * @return 无
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Comment comment) {
        if (comment.getId() == null) {
            // 新增
            commentService.save(comment);
        } else {
            // 修改
            commentService.update(comment);
        }
        return Result.success();
    }

    /**
     * 根据id删除
     *
     * @param id 主键
     * @return 无
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id) {
        commentService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids 主键数组
     * @return 无
     */
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(Integer[] ids) {
        commentService.deleteBatch(ids);
        return Result.success();
    }
}